package com.stone.business.gzrwd.action;

/**
 * <pre>
 * 工作任务单
 * @author 
 * </pre>
 */

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.plan.pojo.TBzgzrw;
import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.sbqxjl.pojo.TSbqxjl;
import com.stone.business.workcard.pojo.MainWorkCardOne;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.sys.service.UserService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.MainUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class GzrwdAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private ZbjlCommonService myService;
	
	private TGzrwd entity;
	
	private String id;
	
	private List<TDictionary> jclbList;
	
	//private List<TGzrwdXcjl> xcjlList;
	
	//private TGzrwdXcjl xcjl;
	
	private String ids;
	private String fxgznr;
	private String hiddenids;
	// 值班长
	List<HashMap<String,Object>> zbzList; 
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	private List<TDictionary> sblxList;
	private List<TOrgan> bzList;
	private TGzrwdRw rw;
	// 任务列表
	private List<TGzrwdRw> rwList = null;
	// 任务列表
	private List<TGzrwd> list = null;
	// 任务列表
	private List<TGzrwdRw> listRwd = null;
	private String isAdmin;
	private String kzklx;
	private String kzkzt;
	private String ckdzt;
	private String gxkzt;
	List<HashMap<String, Object>> gzpnoList;
	List<HashMap<String, Object>> kzkztMap;
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		
		// 修改进入
		if (!"0".equals(id)) {
			
			this.entity = (TGzrwd)myService.findById(Long.parseLong(id), TGzrwd.class.getName());
			
			super.getSession().setAttribute("gzrwdid", entity.getId());
		
			// 任务
			 listRwd = myService.findListByHql("from TGzrwdRw where gzrwdid = '" + entity.getId() + "'");
			/*// 运行工作票号
				String sql="select t.ph as itemId,t.ph as itemName from TAB_GZPTJJL t where  to_char(t.xkgzsj,'yyyy-mm-dd')='"+DateFormatTool.formate(this.entity.getRq(), "yyyy-MM-dd")+"' and t.bdzname like '%"+this.entity.getBdzname()+"%'";		
				UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
			    gzpnoList=userServiceDIMIS.findListBySql(sql);
				*/
			
			// 负责人
			 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
				
			// 新建进入
		} else {
			
			listRwd = new ArrayList<TGzrwdRw>();

			this.entity = new TGzrwd();
			this.entity.setId(Long.parseLong("0"));
			this.entity.setState(super.getSearchBean().getCon1());
			//this.entity.setState("0");
			entity.setPlanid(Long.parseLong(this.getHiddenids()));
			
			TPlan tplan = (TPlan)myService.findById(this.entity.getPlanid(), TPlan.class.getName());
			this.entity.setBdz(tplan.getStationid());
			this.entity.setBdzname(tplan.getStationname());
			this.entity.setDwry(tplan.getDwry());
			this.entity.setDwryname(tplan.getDwryname());
			this.entity.setCzr(tplan.getYsr());
			this.entity.setCzrname(tplan.getYsrname());
			this.entity.setBdzname(tplan.getStationname());
			this.entity.setZbz(tplan.getFzr());
			this.entity.setZbzname(tplan.getResponsible());
			this.entity.setGcjs(tplan.getGcjs());//20130516工程建设工时*1.2
			String bzid=((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getId().toString();
			if(bzid.equals("21")||bzid.equals("22")||bzid.equals("23")||bzid.equals("24")){//各班组起草工单
				//TPlanBz tplanbz = (TPlanBz)myService.findById(bzid, TPlanBz.class.getName());
				List bzlist = myService.findListByHql("from TPlanBz where delflag='0' and bzid =" + bzid+" and planid="+tplan.getId());
				if(bzlist.size()!=0){
				TPlanBz tplanbz =(TPlanBz)bzlist.get(0);
				this.entity.setBgzr(bzid);//起草任务单班组
				this.entity.setZbyname(tplanbz.getCyname());
				this.entity.setZby(tplanbz.getCyid());	
				this.entity.setZbz(tplanbz.getFzrid());
				this.entity.setZbzname(tplanbz.getFzrname());
				List<TBzgzrw> bzgzrwList = myService.findListByHql("from TBzgzrw where bzid =" + bzid+" and planid="+tplan.getId());	
				for (int i =0;i<bzgzrwList.size();i++) {
							TBzgzrw tbzgzrw = bzgzrwList.get(i);
							TGzrwdRw rw = new TGzrwdRw();
							// 将班组工作任务复制给工作任务单任务
							MainUtils.copy(tbzgzrw, rw);
							rw.setId(Long.parseLong("0"));
							rw.setBzid(tbzgzrw.getBzid());
							rw.setGzrwdid(entity.getId());
							listRwd.add(rw);
				}
				}
							
			}else{//多班组专工起草工单
				//TPlanBz tplanbz = (TPlanBz)myService.findById(bzid, TPlanBz.class.getName());
				List bzlist = myService.findListByHql("from TPlanBz where delflag='0' and planid="+tplan.getId());
				for (int j =0;j<bzlist.size();j++) {
				TPlanBz tplanbz =(TPlanBz)bzlist.get(j);
				//this.entity.setBgzr(bzid);//起草任务单班组
				//this.entity.setZbyname(tplanbz.getCyname());
				//this.entity.setZby(tplanbz.getCyid());	
				List<TBzgzrw> bzgzrwList = myService.findListByHql("from TBzgzrw where bzid =" + tplanbz.getBzid()+" and planid="+tplan.getId());	
				for (int i =0;i<bzgzrwList.size();i++) {
					TBzgzrw tbzgzrw = bzgzrwList.get(i);
					TGzrwdRw rw = new TGzrwdRw();
					// 将班组工作任务复制给工作任务单任务
					MainUtils.copy(tbzgzrw, rw);
					rw.setBzid(tbzgzrw.getBzid());
					rw.setId(Long.parseLong("0"));
					rw.setGzrwdid(entity.getId());
					listRwd.add(rw);
				}
							
			  }
			}
			
			this.entity.setYcclqk(tplan.getPowerrange());
			this.entity.setCzkssj(tplan.getStartdate());
			this.entity.setCzjssj(tplan.getEnddate());
			// 负责人
			//zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'BZZ')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
			zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group'or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
			String organid = ((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getId().toString();
			
			/*for (HashMap<String, Object> map : zbzList) {
				if (organid.equals(map.get("organid").toString())) {
					//this.entity.setZbz(map.get("userid").toString());
					//this.entity.setZbzname(map.get("username").toString());
				
					this.entity.setZbz(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId().toString());
					this.entity.setZbzname(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
					break;
				}
			}*/
			List<TGzrwd> gzrwdPreList = myService.findListByHql(" from TGzrwd where id=(select max(id) as id from TGzrwd where bgzr="+organid + " and planid="+this.entity.getPlanid() + ")" );
			if(gzrwdPreList.size()>0){
			TGzrwd gzrwdPre=gzrwdPreList.get(0);
			MainUtils.copy(gzrwdPre, this.entity);
			this.entity.setRq((DateFormatTool.dateFormatDate(DateFormatTool.addDate(gzrwdPre.getRq(), 1), false)));
			this.entity.setBh("");
			this.entity.setId(0L);
			}else{
				this.entity.setRq(DateFormatTool.dateFormatDate(tplan.getStartdate(),false));
			}
				
			// 日期
			//this.entity.setRq(DateFormatTool.formatToDate(myService.getSysdate("yyyy-MM-dd HH24:mi"),true));
			//this.entity.setOuid(String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE)));
			  //by wangtao update20110319
			this.entity.setOuid(((TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganCode().toString());
			super.getRequest().setAttribute("GZRWDLIST", new ArrayList<HashMap<String, Object>>());
			
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			
			// by wangtao update20110105
			String sysdate = myService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = myService.findListBySql("select MAX(bh) as bh from TAB_GZRWD where bh like '" + sysdate + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setBh(sysdate + "0001");
				
			} else {
				if(this.entity.getBh()==null||this.entity.getBh().equals("")){
				this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			
				}
			
			}

		}
		
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		//HttpSession session= super.getSession();
		setDictionary();
		return SUCCESS;
	}
	/**
	 * 设备缺陷—>工作计划->任务单进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initRwdDetail() throws AppException {
		
		// 修改进入
		if (!"0".equals(id)) {
			//id 设备缺陷id
			List bzgzrwList = myService
			.findListBySql("select planid as planid from tab_bzgzrw a where sbqxid = (select id from tab_sbqxjl where defectid= '"
					+ id + "') order by id desc");
			List<TGzrwd> gzrwdList=null;
	if (bzgzrwList.size() >= 1) {
		HashMap map = (HashMap) bzgzrwList.get(0);
		 gzrwdList = myService
		.findListByHql(" from TGzrwd a where planid = '"
				+ ((BigDecimal) map.get("planid")).intValue() + "'");
	}
			
			//this.entity = (TGzrwd)myService.findById(Long.parseLong(id), TGzrwd.class.getName());
	       if (gzrwdList.size() >= 1) {      
	          this.entity =(TGzrwd)gzrwdList.get(0);
	       }else{
	    	   this.entity = new TGzrwd();
	       }
			super.getSession().setAttribute("gzrwdid", entity.getId());
		
			// 任务
			 listRwd = myService.findListByHql("from TGzrwdRw where gzrwdid = '" + entity.getId() + "'");
			/*// 运行工作票号
				String sql="select t.ph as itemId,t.ph as itemName from TAB_GZPTJJL t where  to_char(t.xkgzsj,'yyyy-mm-dd')='"+DateFormatTool.formate(this.entity.getRq(), "yyyy-MM-dd")+"' and t.bdzname like '%"+this.entity.getBdzname()+"%'";		
				UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
			    gzpnoList=userServiceDIMIS.findListBySql(sql);
				*/
			
			// 负责人
			 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
				
			// 新建进入
		} else {}
		
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		//HttpSession session= super.getSession();
		setDictionary();
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		setDictionary();
		// 新建时
		if (entity.getId() == 0) {
			
			//entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setCjsj(DateFormatTool.formatToDate(sysdate,true));
		//	hiddenids=this.getRequest().getParameter(hiddenids);
			//String[] indexs = hiddenids.split(",");
			//entity.setPlanid(Long.parseLong(indexs[1]));
			entity.setPlanid(Long.parseLong(hiddenids));
			// by wangtao update20120429
			String sysdate1 = myService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = myService.findListBySql("select MAX(bh) as bh from TAB_GZRWD where bh like '" + sysdate1 + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setBh(sysdate1 + "0001");
				
			} else {
				this.entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			}
			myService.save(entity);	
		} else {
			entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setXgrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
			myService.update(entity);
		}
		
		super.getSession().setAttribute("gzrwdid", entity.getId());
		
		// 任务
		//List<TGzrwdRw> listRwd = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		//super.getRequest().setAttribute("GZRWDLIST", listRwd);
		/*for(TGzrwdRw d:listRwd){
			System.out.println("结果:"+d.getCzid());
		}*/
		myService.savePlanMonth(this.entity.getId(),this.listRwd);
		
		// 风险点
	//	List<TGzrwdFxgz> listFxgz = myService.findListByHql("from TGzrwdFxgz where gzrwdid =" + entity.getId() + " order by id");
	//	super.getRequest().setAttribute("FXGZLIST", listFxgz);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		/*// 运行工作票号
			String sql="select t.ph as itemId,t.ph as itemName from TAB_GZPTJJL t where  to_char(t.xkgzsj,'yyyy-mm-dd')='"+DateFormatTool.formate(this.entity.getRq(), "yyyy-MM-dd")+"' and t.bdzname like '%"+this.entity.getBdzname()+"%'";		
			UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
		    gzpnoList=userServiceDIMIS.findListBySql(sql);*/
			
		// 获得字典
//		getDic();
		// 任务
		 listRwd = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		 isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		myService.doDelAll(id, "TGzrwd");
		
		return "DEL-SUCCESS";
		
	}
	
	/**
	 * 查询字典项目
	 */
	public void getDic() {
		
		// 检查类别
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		jclbList = dicService.findList("JCLB");
		
	}

	/*------------------------- 行车记录 -------------------------*/
	/**
	 * 获得行车记录
	 * @return
	 */
	/*public String getGzrwdXcjlList() {
		xcjlList = myService.findListByHql(" from TGzrwdXcjl where gzrwdid = " + super.getSession().getAttribute("gzrwdid"));
		return "GZRWD-XCJL-SUCCESS";
	}*/

	/**
	 * 保存行车记录
	 * @return
	 * @throws AppException
	 */
	/*public String saveXcjl() throws AppException {
		
		// 系统时间
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		
		xcjl.setGzrwdid((Long)super.getSession().getAttribute("gzrwdid"));

		// 新建时
		if ( xcjl.getId() == null || xcjl.getId() == 0) {

			xcjl.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//xcjl.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			xcjl.setCjsj(DateFormatTool.formatToDate(sysdate,true));
			myService.save(xcjl);	
			
		} else {
			xcjl.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//xcjl.setXgrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			xcjl.setXgsj(DateFormatTool.formatToDate(sysdate,true));
			myService.update(xcjl);
		}
		
		super.setInfo("保存成功");
		
		return getGzrwdXcjlList();
	}
*/
	
	/**
	 * 删除行车记录
	 * @return
	 * @throws AppException
	 */
	/*public String deleteXcjl() throws AppException {
		
		myService.deleteAll(id, "TGzrwdXcjl");
		
		return getGzrwdXcjlList();
		
	}*/
	/**
	 * 手动归档
	 * @return
	 * @throws AppException
	 */
	public String history() throws AppException {
		
//		 状态修改
		entity.setState("9");
		myService.update(entity);
       super.getSession().setAttribute("gzrwdid", entity.getId());
		
		// 任务
		List<TGzrwdRw> listRwd = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd);
		
		// 风险点
		//List<TGzrwdFxgz> listFxgz = myService.findListByHql("from TGzrwdFxgz where gzrwdid =" + entity.getId() + " order by id");
		//super.getRequest().setAttribute("FXGZLIST", listFxgz);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		
		super.setInfo("归档成功");
		
		return SUCCESS;
	}
	/*------------------------- 工作任务单 -------------------------*/
	public List<HashMap<String, Object>> getDbList2() throws AppException {
		
//		List<HashMap<String,Object>> daylist = myService.findListBySql("select to_char(sysdate,'day') as aqhdday from dual");
//		
//		if ("星期五".equals(daylist.get(0).get("aqhdday"))) {
//			
//			Date sysdate = DateFormatTool.formatToDate(myService.getSysdate("yyyy-MM-dd"),false);
//			TOrgan o = (TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN);
//			long count = myService.getTotalByDHql("hdrq = ?",  new Object[]{sysdate}, "TZaqhdqk");
//			
//			if (count == 0) {
//			
//				TZaqhdqk aqhdqk = new TZaqhdqk();
//				aqhdqk.setHdrq(sysdate);
//				aqhdqk.setOuid(o.getOrganCode().toString());
//				aqhdqk.setBdz(o.getId());
//				aqhdqk.setBdzname(o.getOrganName());
//				aqhdqk.setState(super.getSearchBean().getCon1());
//				myService.save(aqhdqk);
//			
//			}
//			
//		}
		
//		// 登录者的organCode 100001 by wangtao add20110101
		/*String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
		List<HashMap<String, Object>> listorganCode = myService.findListBySql(" SELECT MAX(PARENT_CODE) AS PARENT_CODE FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 AND SYSDATE >= XCXSSJ " );
		organCode=listorganCode.get(0).get("parent_code").toString();*/
//		// 是否管理员
//		String isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
//		 区分中心操作队organCode1 100001 by wangtao add20110312
		String organCode1 = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
		//String isLike = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_ISLIKE));
		
		String conditions = "";
		//conditions = " AND JL.OUID "+isLike+" '%," + organCode1 + "%,%'";
//		// 查询条件OUID很重要
	   //if (!"1".equals(isAdmin)) {
		//conditions = " AND JL.OUID like '%," + organCode1 + "%,%'";
		//}
		
		/*StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM (");
		// 异常故障处理任务（处理时间未填写）
		sql.append("SELECT BDZNAME || ' ' || TO_CHAR(TZSJ,'YYYY-MM-DD HH24:MI') || YCNR AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" '8' AS MODEL,");
		sql.append(" 'ycgz' AS CZURL,");
		sql.append(" BDZNAME AS BDZNAME,");
		sql.append(" ID AS ID");
		sql.append(" FROM TAB_YCGZ JL");
		sql.append(" WHERE CLSJ IS NULL AND GZRWDCONFIRM = '0' "); //  
		sql.append(conditions);
		
		// 操作任务
		sql.append(" UNION ");
		sql.append("SELECT YG.BDZNAME  || '计划操作开始时间：' || TO_CHAR(YG.JHKSCZSJ,'YYYY-MM-DD HH24:MI') || JL.CZRW AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" '6' AS MODEL,");
		sql.append(" 'dzczjl' AS CZURL,");
		sql.append(" YG.BDZNAME AS BDZNAME,");
		sql.append(" JL.ID AS ID");
		sql.append(" FROM TAB_DZCZYG YG, TAB_DZCZJL JL ");
		sql.append(" WHERE YG.ID = JL.DZCZYGID AND JL.STATE = '0' AND JL.CZJSSJ IS NULL AND JL.GZRWDCONFIRM = '0' ");//AND GZRWDCONFIRM = '0' 
		sql.append(conditions);
		
		-
		 * 工作票 许可任务 计划工作开始时间==系统日期 或者 计划工作结束时间== 系统日期。  
		 * 提取数据：变电站 工作任务 计划工作时间 计划工作结束时间 
		 
		sql.append(" UNION ");
		sql.append("SELECT BDZNAME ||':'|| GZRW || '许可、 终结计划时间：' || TO_CHAR(JHGZSJKS,'YYYY-MM-DD HH24:MI') || '~' || TO_CHAR(JHGZSJJS,'YYYY-MM-DD HH24:MI') AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" '3' AS MODEL,");
		sql.append(" 'gzptjjl' AS CZURL,");
		sql.append(" BDZNAME AS BDZNAME,");
		sql.append(" ID AS ID");
		sql.append(" FROM TAB_GZPTJJL JL");
		sql.append(" WHERE XKGZSJ IS NULL AND (TO_CHAR(JHGZSJJS,'YYYY-MM-DD') = TO_CHAR(SYSDATE,'YYYY-MM-DD') ");
		sql.append(" OR TO_CHAR(JHGZSJKS,'YYYY-MM-DD') = TO_CHAR(SYSDATE,'YYYY-MM-DD')) AND JL.STATE = '0' AND GZRWDCONFIRM = '0' ");
		sql.append(conditions);
		
		-
		 * 工作票 终结任务 计划工作结束时间== 系统日期， 并且没有终结的.
		 * 三种情况 第一种当天其实未终结的。 提前完成的不提示了， 工作票延期按工作票延期时间，不按计划结束时间 
		 
		sql.append(" UNION ");
		sql.append(" SELECT CASE WHEN JL.GZPYQSJ IS NULL THEN JL.BDZNAME ||':'|| GZRW || ' 计划结束时间:' || TO_CHAR(JHGZSJJS, 'YYYY-MM-DD HH24:MI') ");
		sql.append("   WHEN TO_CHAR(JL.GZPYQSJ, 'YYYY-MM-DD HH24:MI') > TO_CHAR(JL.JHGZSJJS, 'YYYY-MM-DD HH24:MI') THEN JL.BDZNAME || GZRW || ' 延期时间：' || TO_CHAR(GZPYQSJ, 'YYYY-MM-DD HH24:MI') 　END AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ, ");
		sql.append("   '4' AS MODEL,");
		sql.append("   'gzptjjl' AS CZURL,");
		sql.append("   JL.BDZNAME AS BDZNAME,");
		sql.append("   JL.ID AS ID ");
		sql.append(" FROM TAB_GZPTJJL JL ");
		//sql.append(" WHERE GZPZJSJ IS NOT NULL AND (TO_CHAR(JHGZSJJS, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') OR ");
		sql.append(" WHERE XKGZSJ IS NOT NULL and GZPZJSJ IS  NULL AND (TO_CHAR(JHGZSJJS, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') OR ");//by wangtao update101231
		sql.append(" TO_CHAR(GZPYQSJ, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') OR GZRWDCONFIRM3 = '1') ");
		sql.append(" AND JL.STATE = '0' ");
		sql.append(" AND GZRWDCONFIRM2 = '0'  ");
		sql.append(conditions);
		-
		 * 正常巡视任务（巡视情况需要回写设置的巡视结束时间（比较时间 设置某个站多长时间巡视一次））
		 * 正常巡视是新建，不是修改，所以ＩＤ存放变电站ＩＤ，ｊｓｐ判断是正常巡视ＩＤ　＝０
		 
//		sql.append(" UNION ");
//		sql.append("　SELECT ORGAN_NAME || '巡视' AS GZRW,");
//		sql.append(" 	'0' AS MODEL,");
//		sql.append(" 	'xsqk' AS CZURL,");	
//		sql.append(" 	ORGAN_NAME AS BDZNAME,");
//		sql.append("    TO_CHAR(ID) AS BDZID,");	
//		sql.append("　	0 AS ID");
//		sql.append(" FROM SYS_ORGAN ");
//		sql.append("  WHERE XCXSSJ IS NOT NULL ");
//		sql.append("  AND MOD((TO_NUMBER(TO_CHAR(XCXSSJ,'YYYYMMDD')) - TO_NUMBER(TO_CHAR(SYSDATE,'YYYYMMDD'))),XSJGSJ) = 0 ");
//		sql.append("  AND ORGAN_CODE LIKE '").append(organCode).append("%'");
//		sql.append("  AND (TO_CHAR(GZRWDDATE,'YYYYMMDD') IS NULL ");
//		sql.append("  OR TO_CHAR(GZRWDDATE,'YYYYMMDD') != TO_CHAR(SYSDATE,'YYYYMMDD'))");
		
		-
		 * 正常巡视任务 计划任务
		 
		sql.append(" UNION ");
		sql.append("　SELECT BDZNAME || '正常巡视' AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" 	'0' AS MODEL,");
		sql.append(" 	'xsqk' AS CZURL,");	
		sql.append(" 	BDZNAME AS BDZNAME,");
		sql.append("　	ID AS ID");
		sql.append(" FROM TAB_XSQK JL");
		sql.append("  WHERE KSSJ IS NULL AND XSLX = '2' AND GZRWDCONFIRM = '0'");
//		sql.append("   AND OUID LIKE '%,").append(organCode).append("%,%'");
		sql.append(conditions);
		// 夜间特巡
		sql.append(" UNION ");
		//sql.append("　SELECT BDZNAME || TO_CHAR(TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI'),'YYYY-MM') || '夜间巡视' AS GZRW,");
		sql.append("　SELECT BDZNAME || '夜间巡视' AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");//by wangtao update20110105
		sql.append(" 	'1' AS MODEL,");
		sql.append(" 	'xsqk' AS CZURL,");	
		sql.append(" 	BDZNAME AS BDZNAME,");
		sql.append("　	ID AS ID");
		sql.append(" FROM TAB_XSQK JL");
		sql.append("   WHERE KSSJ IS NULL AND XSLX = '3' AND GZRWDCONFIRM = '0' ");
//		sql.append("   AND OUID LIKE '%,").append(organCode).append("%,%'");
		//sql.append("  AND OUID LIKE '%").append(organCode).append("%'");//by wangtao add 20110101
		//sql.append("   AND BDZ NOT IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 )");
		//sql.append("  AND OUID LIKE '%").append(organCode).append("%'");//by wangtao add 20110101
		//by wangtao add20110322
		sql.append("   AND BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN  WHERE XCXSSJ IS NOT NULL AND PARENT_CODE IN (SELECT TO_CHAR(PARENT_CODE) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 AND SYSDATE >= XCXSSJ ))");
		sql.append("   AND (BDZ NOT IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 )");
		sql.append("   OR BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND DYDJ='220' AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 ))");
		
		sql.append(conditions);
		
		-
		 *监督性巡视任务 计划任务
		 
		sql.append(" UNION ");
		sql.append("　SELECT BDZNAME || '监督性巡视' AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" 	'0' AS MODEL,");
		sql.append(" 	'xsqk' AS CZURL,");	
		sql.append(" 	BDZNAME AS BDZNAME,");
		sql.append("　	ID AS ID");
		sql.append(" FROM TAB_XSQK JL");
		sql.append("  WHERE KSSJ IS NULL AND XSLX = '5' AND GZRWDCONFIRM = '0'");
//		sql.append("   AND OUID LIKE '%,").append(organCode).append("%,%'");
		sql.append("   AND (BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 OR MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -4 )");//主班
		sql.append("   OR BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -1 OR MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -5))");//副班
		sql.append(conditions);
		
		// 设备试验轮换  每次是否对应多个变电站?????计划任务都建上GZRWDCONFIRM
		sql.append(" UNION ");
		sql.append("　SELECT BDZNAME || TO_CHAR(RQ,'YYYY-MM') || REPLACE(REPLACE(DIC.ITEM_NAME,'110kV及以下站',''),'220kV站','') AS GZRW,TO_CHAR(JL.CJSJ,'YYYY-MM-DD HH24:MI') AS CJSJ,");
		sql.append(" 	'A' AS MODEL,");
		sql.append(" 	'sbsylh' AS CZURL,");	
		sql.append(" 	BDZNAME AS BDZNAME,");
		sql.append("　	ID AS ID");
		sql.append(" FROM TAB_SBSYLH JL, SYS_DICTIONARY DIC");
		sql.append("  WHERE JL.NR = DIC.ITEM_ID AND DIC.ITEM_TYPE = 'SBSYLH' AND RQ IS NULL AND GZRWDCONFIRM = '0' ");
//		sql.append("   AND OUID LIKE '%,").append(organCode).append("%,%'");
		sql.append("   AND (BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = 0 OR MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -4 )");
		sql.append("   OR JL.NR='0019' AND BDZ IN(SELECT TO_CHAR(ID) FROM SYS_ORGAN WHERE XCXSSJ IS NOT NULL AND MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -1 OR MOD((TO_date(TO_CHAR(XCXSSJ,'YYYYMMDD'),'YYYYMMDD') - TO_date(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD')),XSJGSJ) = -5 ))");//副班变电站运行主人监督﹝防火、防汛、安全工器具检查﹞
		sql.append(conditions);*/
		
//		// 周安全活动情况
//		sql.append(" UNION ");
//		sql.append("　SELECT '周安全活动情况:' || BDZNAME || TO_CHAR(HDRQ,'YYYY-MM-DD') || '请上传周安全活动情况录音' AS GZRW,");		
//		sql.append(" 'D' AS MODEL,");
//		sql.append(" 'zaqhdqk' AS CZURL,");
//		sql.append("  BDZNAME AS BDZNAME,");
//		sql.append("　ID AS ID");
//		sql.append(" FROM TAB_ZAQHDQK ");
//		sql.append("  WHERE LY = 0 AND GZRWDCONFIRM = '0'");

//		if (!"1".equals(isAdmin)) {
//			sql.append(" AND OUID = '").append(organCode).append("'");
//		}
//		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM (");
		// 待办任务（处理时间未填写）
		sql.append("SELECT RWNR AS GZRW,");
		sql.append(" '8' AS MODEL,");
		sql.append(" 'ycgz' AS CZURL,");
		sql.append(" BDZNAME AS BDZNAME,");
		sql.append(" ID AS ID");
		sql.append(" FROM TAB_GZRWD_RW JL");
		sql.append(" WHERE  SFWC = '0' "); //  
		sql.append(conditions);
		sql.append(") ORDER BY MODEL, BDZNAME");
		List<HashMap<String, Object>> listRwd = myService.findListBySql(sql.toString());
		
		return listRwd;
		
	}
	
	public String getDbList() throws AppException {
			
		List<HashMap<String, Object>> listRwd = getDbList2();
		
		super.getSession().setAttribute("DBRWDLIST", listRwd);
		
		return "GZRWD-DB-LIST";
		
	}
	
	/**
	 * 通过任务保存
	 * @return
	 * @throws AppException
	 */
	public String saveRwd() throws AppException {
		
		//mySerice.saveGzrwdRwdList();
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		
		// 新建时
		if (entity.getId() == 0) {
			
			/*List<HashMap<String, Object>> list = myService.findListBySql("select MAX(bh) as bh from TAB_GZRWD where bh like '" + entity.getBh().substring(0,6) + "%'");
			
			// 重新取得票号
			if (list.get(0).get("bh") != null && !StringUtils.isNullOrBlank(list.get(0).get("bh").toString())) {
				entity.setBh(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			}*/
			entity.setState(super.getSearchBean().getCon1());
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setCjsj(DateFormatTool.formatToDate(sysdate,true));
		
		//	myService.save(entity);	
		
		} else {
			entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setXgrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
			//myService.update(entity);
		}
		
		super.getSession().setAttribute("gzrwdid", entity.getId());
		
		List<HashMap<String, Object>> listRwd =  (ArrayList<HashMap<String, Object>>) super.getSession().getAttribute("DBRWDLIST");
		
		// 保存工作任务单要保存　主表　工作任务单子表　相应业务表的是否出了工作单字段
		myService.saveGzrwdRw(ids, listRwd, entity,DateFormatTool.formatToDate(sysdate,true));
		
		// 任务
		List<TGzrwdRw> listRwd2 = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd2);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		
		super.getSession().removeAttribute("DBRWDLIST");

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	
	/**
	 * 通过风险告知保存
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public String saveFxgz() throws AppException {
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");

		entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getLoginId());
		entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
		
		// 保存风险告知
		myService.saveGzrwdFxgz(entity,ids, super.getRequest().getParameter("idsalready"));
		
		// 任务
		List<TGzrwdRw> listRwd2 = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd2);
		
		// 风险点
		//List<TGzrwdFxgz> listFxgz = myService.findListByHql("from TGzrwdFxgz where gzrwdid =" + entity.getId() + " order by id");
		//super.getRequest().setAttribute("FXGZLIST", listFxgz);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		
		super.getSession().removeAttribute("DBRWDLIST");

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	/**
	 * 修改风险告知
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public String updateFxgz() throws AppException {
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");

		entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getLoginId());
		entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
		
		// 修改风险告知
		//myService.saveGzrwdFxgz(entity,ids, super.getRequest().getParameter("idsalready"));
	//	myService.updateFxgznr(ids,fxgznr);
		// 任务
		List<TGzrwdRw> listRwd2 = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd2);
		
		// 风险点
		//List<TGzrwdFxgz> listFxgz = myService.findListByHql("from TGzrwdFxgz where gzrwdid =" + entity.getId() + " order by id");
		//super.getRequest().setAttribute("FXGZLIST", listFxgz);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		
		super.getSession().removeAttribute("DBRWDLIST");

		super.setInfo("修改成功");
		
		return SUCCESS;
	}
	/**
	 * 工作任务单 未完成时处理
	 */
	public void updateRw() throws AppException {
		
		// 
		myService.updateGzrwdRwUnOver(this.id, super.getRequest().getParameter("valuesfwc"));

	}
	
	/**
	 * 删除工作任务或者风险告知
	 */
	public void deleteDetail() throws AppException {
		
		 // 任务
		if (!"TGzrwdFxgz".equals(super.getRequest().getParameter("model"))) {
			myService.updateGzrwdRwCancel(Long.parseLong(id));
			
			// 风险告知
		} else {
			myService.deleteAll(id, super.getRequest().getParameter("model"));
		}
		
	}
	
	/**
	 * 打印
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public String print() throws AppException {
		
		this.entity = (TGzrwd)myService.findById(Long.parseLong(id), TGzrwd.class.getName());
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		this.entity.setPcsj(DateFormatTool.formatToDate(sysdate,true));
		
		myService.update(entity);
		
		// 任务
		List<TGzrwdRw> listRwd2 = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd2);
		
		// 风险点
		//List<TGzrwdFxgz> listFxgz = myService.findListByHql("from TGzrwdFxgz where gzrwdid =" + entity.getId() + " order by id");
	//	super.getRequest().setAttribute("FXGZLIST", listFxgz);
		
		super.getSession().removeAttribute("DBRWDLIST");
		/*
		this.entity = (TGzrwd)myService.findById(Long.parseLong(id), TGzrwd.class.getName());
		xcjlList = myService.findListByHql(" from TGzrwdXcjl where gzrwdid = " + entity.getId());
		
		*/
		return "PRINT-SUCCESS";
	}
	/**
	 * 删除代办任务
	 * @return
	 * @throws AppException
	 */
	public String deleteRwd() throws AppException {
		
		super.getSession().setAttribute("gzrwdid", entity.getId());
		
		List<HashMap<String, Object>> listRwd =  (ArrayList<HashMap<String, Object>>) super.getSession().getAttribute("DBRWDLIST");
		
		// 删除工作任务单
		myService.deleteGzrwdRw(ids, listRwd);
		
		// 任务
		List<TGzrwdRw> listRwd2 = myService.findListByHql("from TGzrwdRw where gzrwdid =" + entity.getId());
		super.getRequest().setAttribute("GZRWDLIST", listRwd2);
		
		// 负责人
		 zbzList = myService.getListByDHql("u.id as userid, u.name as username, u.organ.id as organid", "u.id = ur.userId and ur.roleId = r.id and (r.nameen = 'ZBZ' or r.nameen = 'group' or r.nameen = 'bhbby' or r.nameen = 'jxbby' or r.nameen = 'byqby' or r.nameen = 'sybby')", null, "group by u.id ,u.name, u.organ.id order by u.organ.id", "TUser u, TUserRole ur , TRole r");
		
		super.getSession().removeAttribute("DBRWDLIST");

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	/**
	 * 获得字典项目
	 */
	private void setDictionary() {
		
		// 变电站
		this.bdzList = myService.findListByHql(" from TBdz order by sortid");
		// 电压等级
		this.dydjList = myService.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 设备类型
		this.sblxList = myService.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
		// 班组
		this.bzList = myService.findListByHql(" from TOrgan where parentId=2 and organNameEn like '%B' order by id");
		/*// 运行工作票号
		String sql="select t.ph as itemId,t.ph as itemName from TAB_GZPTJJL t where  to_char(t.xkgzsj,'yyyy-mm-dd')='"+DateFormatTool.formate(this.entity.getRq(), "yyyy-MM-dd")+"' and t.bdzname like '%"+this.entity.getBdzname()+"%'";		
		UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
	    try {
	    	gzpnoList=userServiceDIMIS.findListBySql(sql);
		} catch (Exception e) {
			// TODO: handle exception
			gzpnoList=new ArrayList<HashMap<String, Object>>();
		}*/
		List<MainWorkCardOne> kzkList = myService.findListByHql("from MainWorkCardOne where planid =" + this.entity.getPlanid());	
		if(kzkList.size()==0){
			kzklx="0";
		}else{
		kzklx=kzkList.get(0).getOc_type();
		}
		//控制卡状态
		List<HashMap<String, Object>> kzkztMap = myService.findListBySql("select nvl(tk.sh_status, '0') as kzkzt from tab_kzk tk  where tk.planid =" + this.entity.getPlanid());	
		if(kzkztMap.size()==0){
			kzkzt="0";
		}else{
			kzkzt=kzkztMap.get(0).get("kzkzt").toString();
		}
		//领料单状态
		List<HashMap<String, Object>> ckdztMap = myService.findListBySql("select nvl(t.status, '0') as ckdzt from tab_cllyjl t  where t.gzrwdid =" + this.entity.getId());	
		if(ckdztMap.size()==0){
			ckdzt="0";
		}else{
			ckdzt=ckdztMap.get(0).get("ckdzt").toString();
		}
		//工序卡状态
		List<HashMap<String, Object>> gxztMap = myService.findListBySql(" select nvl((select case tg.oc_status when '1' then (select case concat(max(oc_gzzt), min(oc_gzzt)) when '0303' then '5'  when '0101' then '6' else '7' end  from tab_gxzlkzk_bz tgbz where tgbz.p_idsno = tg.id_sno) else tg.oc_status end from tab_gxzlkzk tg where tg.planid =" + this.entity.getPlanid()+"),'0') as gxkzt from tab_gxzlkzk tg where tg.planid =" + this.entity.getPlanid());	
		if(gxztMap.size()==0){
			gxkzt="0";
		}else{
			gxkzt=gxztMap.get(0).get("gxkzt").toString();
		}
		
	}
	public void getGzpno() throws Exception {

		try {

			String bdzname = super.getRequest().getParameter("bdzname");
			String rq = super.getRequest().getParameter("rq");
			String formatStr="yyyy-MM-dd";
			// 运行工作票号
			String sql="select t.ph as itemId,t.ph as itemName from TAB_GZPTJJL t where  to_char(t.xkgzsj,'yyyy-MM-dd')='"+DateFormatTool.formate(DateFormatTool.formatToDate(rq, false),formatStr)+"' and t.bdzname like '%"+bdzname+"%'";		
			UserService userServiceDIMIS = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
			gzpnoList=userServiceDIMIS.findListBySql(sql);
		} catch (Exception e) {
			gzpnoList=new ArrayList<HashMap<String, Object>>();
		}
		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");
		JSON json = JSONSerializer.toJSON(gzpnoList, jsonConfig);
		super.getResponse().getWriter().print(json.toString());

	}
	/**
	 * by wangtao add20130508安排工作任务选择人员时进行提醒。
	 */
	public void gzapFzrCheck() throws Exception {
		List userList=null;
		try {
			String fzrid = super.getRequest().getParameter("zbz");
			String rq = super.getRequest().getParameter("rq");
			String formatStr="yyyy-MM-dd";
			// 已经安排任务的负责人
			String sql="select * from tab_gzrwd t where  to_char(t.rq,'yyyy-MM-dd')='"+DateFormatTool.formate(DateFormatTool.formatToDate(rq, false),formatStr)+"' and t.zbz = '"+fzrid+"'";		
			 userList=myService.findListBySql(sql);
		} catch (Exception e) {
			
		}
		int count=0;
		if(userList != null){
			count = userList.size();
		}
		if (count == 0) {
			super.getResponse().getWriter().print("{count:0}");
		} else {
			super.getResponse().getWriter().print("{count:1}");
		}
	}
	/**
	 * by wangtao add20130508安排工作任务选择人员时进行提醒。
	 */
	public void gzapCyCheck() throws Exception {
		List userList=null;
		try {
			String zbyname = super.getRequest().getParameter("zby");
			String rq = super.getRequest().getParameter("rq");
			String formatStr="yyyy-MM-dd";
			// 已经安排任务的负责人
			String sql="select * from tab_gzrwd t where  to_char(t.rq,'yyyy-MM-dd')='"+DateFormatTool.formate(DateFormatTool.formatToDate(rq, false),formatStr)+"' and t.zbyname like '%"+zbyname+"%'";		
			 userList=myService.findListBySql(sql);
		} catch (Exception e) {
			
		}
		int count=0;
		if(userList != null){
			count = userList.size();
		}
		if (count == 0) {
			super.getResponse().getWriter().print("{count:0}");
		} else {
			super.getResponse().getWriter().print("{count:1}");
		}
	}
	/**
	 * 保存任务
	 * @return
	 * @throws AppException
	 */
	public String saveRw() throws AppException {
		
		// 系统时间
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		
		rw.setGzrwdid((Long)super.getSession().getAttribute("gzrwdid"));

		super.setInfo("保存成功");
		
		return getCzrw();
	}

	
	
	/**
	 * 删除任务
	 * @return
	 * @throws AppException
	 */
	public String deleteRw() throws AppException {
		
		myService.deleteGzrwdRw(id);
		
		return getCzrw();
		
	}
	public void deleteRwdDetail() throws Exception {
		myService.doDelAll(id, "TGzrwdRw");
        super.getResponse().getWriter().print("{msg:'删除成功'}");
	}

	/**
	 * 任务列表
	 * @return
	 * @throws AppException
	 */
	public String getCzrw() throws AppException {
		
		rwList = myService.findListByHql(" from TGzrwdRw where gzrwdid = " + super.getSession().getAttribute("gzrwdid"));
		
		return "CZRW-SUCCESS";
		
	}
	/**
	 * 月计划对应的工作任务单列表
	 * @return
	 * @throws AppException
	 */
	public String getGzrwdList() throws AppException {
		
		list = myService.findListByHql(" from TGzrwd where planid = " + id);
		
		return "GZRWD-LIST-SUCCESS";
		
	}
	/**
	 * 新建工单前验证当天工单存在否
	 * 
	 * @throws Exception
	 */
	public void initPermitCheck() throws Exception {
		String organid=((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getOrgan().getId()+"";
		/*long count = myService
				.getTotalBySql("select count(*) as count from TAB_GZRWD where planid in ("
						+ getHiddenids() + ") and to_char(rq,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD') and bgzr="+ organid);*/
		List list=myService.findListBySql("select count(*) as count from TAB_GZRWD where planid in ("
				+ getHiddenids() + ") and to_char(rq,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD') and bgzr="+ organid);
		 HashMap hashmap = (HashMap)list.get(0);
		//long count=hashmap.get("count").toString();
		long count=Long.valueOf(Long.parseLong(String.valueOf(hashmap.get("count"))));
		super.getResponse().getWriter().print(count);

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.myService = myService;
	}

	public List<TDictionary> getJclbList() {
		return jclbList;
	}

	public TGzrwd getEntity() {
		return entity;
	}

	public void setEntity(TGzrwd entity) {
		this.entity = entity;
	}

	/*public List<TGzrwdXcjl> getXcjlList() {
		return xcjlList;
	}

	public void setXcjl(TGzrwdXcjl xcjl) {
		this.xcjl = xcjl;
	}

	public TGzrwdXcjl getXcjl() {
		return xcjl;
	}*/

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<HashMap<String, Object>> getZbzList() {
		return zbzList;
	}

	public String getFxgznr() {
		return fxgznr;
	}

	public void setFxgznr(String fxgznr) {
		this.fxgznr = fxgznr;
	}


	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public void setBdzList(List<TBdz> bdzList) {
		this.bdzList = bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public void setDydjList(List<TDictionary> dydjList) {
		this.dydjList = dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}

	public List<TOrgan> getBzList() {
		return bzList;
	}

	public void setBzList(List<TOrgan> bzList) {
		this.bzList = bzList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setJclbList(List<TDictionary> jclbList) {
		this.jclbList = jclbList;
	}

	public void setZbzList(List<HashMap<String, Object>> zbzList) {
		this.zbzList = zbzList;
	}
	public String getHiddenids() {
		if (this.hiddenids != null) {
			if (!StringUtils.isNullOrBlank(this.hiddenids)) {
				if (this.hiddenids.startsWith(",")) {
					this.hiddenids = this.hiddenids.substring(1, this.hiddenids
							.length());
				}
				if (this.hiddenids.endsWith(",")) {
					this.hiddenids = this.hiddenids.substring(0, this.hiddenids
							.length() - 1);
				}
			}
		}
		return this.hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

	public TGzrwdRw getRw() {
		return rw;
	}

	public void setRw(TGzrwdRw rw) {
		this.rw = rw;
	}

	public List<TGzrwdRw> getRwList() {
		return rwList;
	}

	public void setRwList(List<TGzrwdRw> rwList) {
		this.rwList = rwList;
	}

	public List<TGzrwd> getList() {
		return list;
	}

	public void setList(List<TGzrwd> list) {
		this.list = list;
	}

	public List<TGzrwdRw> getListRwd() {
		return listRwd;
	}

	public void setListRwd(List<TGzrwdRw> listRwd) {
		this.listRwd = listRwd;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}

	public List<HashMap<String, Object>> getGzpnoList() {
		return gzpnoList;
	}

	public void setGzpnoList(List<HashMap<String, Object>> gzpnoList) {
		this.gzpnoList = gzpnoList;
	}

	public String getKzklx() {
		return kzklx;
	}

	public void setKzklx(String kzklx) {
		this.kzklx = kzklx;
	}

	public String getKzkzt() {
		return kzkzt;
	}

	public void setKzkzt(String kzkzt) {
		this.kzkzt = kzkzt;
	}

	public List<HashMap<String, Object>> getKzkztMap() {
		return kzkztMap;
	}

	public void setKzkztMap(List<HashMap<String, Object>> kzkztMap) {
		this.kzkztMap = kzkztMap;
	}

	public String getCkdzt() {
		return ckdzt;
	}

	public void setCkdzt(String ckdzt) {
		this.ckdzt = ckdzt;
	}

	public String getGxkzt() {
		return gxkzt;
	}

	public void setGxkzt(String gxkzt) {
		this.gxkzt = gxkzt;
	}

	
}
